package org.eae.eldf.webapp.controller.module;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.eae.eldf.webapp.vo.PageListVo;
import org.eae.eldf.dto.module.ModulePurviewQueryDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.entity.module.ModuleMenu;
import org.eae.eldf.entity.module.ModulePurview;
import org.eae.eldf.entity.module.ModuleRolePurview;
import org.eae.eldf.entity.sys.SysOperator;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleMenuService;
import org.eae.eldf.service.module.ModulePurviewService;
import org.eae.eldf.service.module.ModuleRolePurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/module")
public class ModulePurviewController extends AbstractController<ModulePurview> {

	private ModulePurviewService modulePurviewService;

	private ModuleRolePurviewService moduleRolePurviewService;

	private ModuleMenuService moduleMenuService;

	@Autowired
	public void setModulePurviewService(ModulePurviewService modulePurviewService) {
		this.modulePurviewService = modulePurviewService;
	}

	@Autowired
	public void setModuleMenuService(ModuleMenuService moduleMenuService) {
		this.moduleMenuService = moduleMenuService;
	}

	@Autowired
	public void setModuleRolePurviewService(ModuleRolePurviewService moduleRolePurviewService) {
		this.moduleRolePurviewService = moduleRolePurviewService;
	}

	/**
	 * 模块权限列表
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/purview/modules")
	@ResponseBody
	public List<ModuleInfo> getModules(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		List<ModuleInfo> listModuleInfos = moduleMenuService.queryModuleList();
		return listModuleInfos;
	}

	/*	*//**
			 * 跳转到模块权限信息页面
			 * 
			 * @param modelMap
			 * @param id
			 * @param searchValue
			 * @return
			 *//*
			 * @RequestMapping(value = "/purview/form")
			 * 
			 * @Token(save = true) public String toProdCategoryPage(ModelMap
			 * modelMap,
			 * 
			 * @RequestParam(required = false) Long id,
			 * 
			 * @RequestParam(required = false) Long moduleMenuId,
			 * 
			 * @RequestParam(required = false) Long moduleInfoId,
			 * 
			 * @RequestParam(required = false) String searchValue) {
			 * ModulePurview modulePurview = null; if (id != null) {
			 * modulePurview = modulePurviewService.get(ModulePurview.class,
			 * id); } else { modulePurview = new ModulePurview();
			 * modulePurview.setModuleMenuId(moduleMenuId); }
			 * modelMap.addAttribute("purview", modulePurview);
			 * modelMap.addAttribute("moduleInfoId", moduleInfoId);
			 * modelMap.addAttribute("searchValue", searchValue); return
			 * "/module/purview/form";
			 * 
			 * }
			 */

	/**
	 * 模块权限列表
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/purview/list")
	public String listPage(HttpServletRequest request, HttpServletResponse response) {

		return "/module/purview/module_purview";
	}

	@RequestMapping(value = "/purview/purviewlist")
	public @ResponseBody ResultList<ModulePurview> menulist(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "modulePurviewQueryDto") ModulePurviewQueryDto modulePurviewQueryDto) {

		Pagination<ModulePurview> pager = super.newPaginationInstance(modulePurviewQueryDto);
		pager = modulePurviewService.pageModulePurviewList(pager, modulePurviewQueryDto);

		return outPageArrayList(pager, modulePurviewQueryDto);
	}

	/**
	 * 模块权限插入
	 * 
	 * @Date : 2016年10月25日
	 * @param request
	 * @param response
	 * @param modulePurview
	 * @return
	 */

	@RequestMapping(value = "/purview/add")
	@ResponseBody
	public Result insert(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "modulePurview") ModulePurview modulePurview) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		modulePurviewService.insert(Criteria.create(ModulePurview.class).set("purCode", modulePurview.getPurCode())
				.set("purName", modulePurview.getPurName()).set("purNameKey", modulePurview.getPurNameKey())
				.set("moduleMenuId", modulePurview.getModuleMenuId()).set("urlPath", modulePurview.getUrlPath())
				.set("purDesc", modulePurview.getPurDesc()).set("createTime", new Date())
				.set("createUser", SpringSecurityUtils.getCurrentUserName()));
		return result;
	}

	/**
	 * 模块权限插入
	 * 
	 * @param id
	 * @param purCode
	 * @param purName
	 * @param purNameKey
	 * @param moduleMenuId
	 * @param urlPath
	 * @param purDesc
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/purview/update")
	@ResponseBody
	public Result update(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "modulePurview") ModulePurview modulePurview) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		// code暂时没有判断唯一
		modulePurviewService.update(Criteria.create(ModulePurview.class).set("purCode", modulePurview.getPurCode())
				.set("purName", modulePurview.getPurName()).set("purNameKey", modulePurview.getPurNameKey())
				.set("moduleMenuId", modulePurview.getModuleMenuId()).set("urlPath", modulePurview.getUrlPath())
				.set("purDesc", modulePurview.getPurDesc()).set("lastUpdateTime", new Date())
				.set("lastUpdateUser", SpringSecurityUtils.getCurrentUserName())
				.where("modulePurviewId", new Object[] { modulePurview.getModulePurviewId() }));

		return result;
	}

	/**
	 * 删除角色
	 */
	@RequestMapping(value = "/purview/del")
	public @ResponseBody Result delete(@RequestParam Long id) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		try {
			moduleRolePurviewService
					.delete(Criteria.create(ModuleRolePurview.class).where("modulePurviewId", new Object[] { id }));
			modulePurviewService.delete(ModulePurview.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据模块id查询子菜单
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/purview/findModule")
	public @ResponseBody List<ModuleMenu> findModule(@RequestParam Long moduleInfoId) {
		Criteria criteria = Criteria.create(ModuleMenu.class).where("moduleInfoId", new Object[] { moduleInfoId });
		List<ModuleMenu> moduleMenuList = moduleMenuService.queryList(criteria);
		return moduleMenuList;
	}

	/**
	 * 根据模块id查询子菜单
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/purview/getPurviewList")
	public @ResponseBody List<ModulePurview> getPurviewList(@RequestParam Long id) {
		Criteria criteria = Criteria.create(ModulePurview.class).where("moduleMenuId", new Object[] { id });
		List<ModulePurview> modulePurviewList = modulePurviewService.queryList(criteria);
		// Map map = new HashMap();
		// map.put("data", modulePurviewList);
		// JSONObject jsonObject =new JSONObject(map);
		return modulePurviewList;
	}

}
